package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.entity.OaLearnEntity;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 学习
 *
 * @author cheer
 */
@Mapper
@Repository
public interface OaLearnDao extends BaseMapper<OaLearnEntity> {

    default IPage<OaLearnEntity> selectPageByPid(Integer pageNum, Integer pageSize, Integer pid) {
        QueryWrapper<OaLearnEntity> queryWrapper = new QueryWrapper<>();

        if (pid != null) {
            queryWrapper.eq("pid", String.valueOf(pid));
        }

        queryWrapper.orderByDesc("id");

        return selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }

    default List<OaLearnEntity> selectList() {
        return selectList(new QueryWrapper<OaLearnEntity>()
                .orderByDesc("add_time"));
    }

    default List<OaLearnEntity> selectListByPid(Integer pid) {
        return selectList(new QueryWrapper<OaLearnEntity>()
                .eq("pid", String.valueOf(pid))
                .orderByDesc("add_time"));
    }

    default OaLearnEntity getFirst() {
        return selectOne(new QueryWrapper<OaLearnEntity>()
                .orderByDesc("id")
                .last("limit 1"));
    }

    default String selectUrlById(Integer id) {
        OaLearnEntity learn = selectOne(new QueryWrapper<OaLearnEntity>()
                .select("url")
                .eq("id", id)
                .last("limit 1"));
        if (learn == null) {
            return null;
        }
        return learn.getUrl();
    }

    default List<OaLearnEntity> list(long startSecond, long endSecond){
        return  selectList(new QueryWrapper<OaLearnEntity>().lambda()
                .ge(OaLearnEntity::getStartTime, startSecond)
                .le(OaLearnEntity::getStartTime, endSecond));
    }
}
